<!-- eslint-disable vue/multi-word-component-names -->
<template>
    <div class="home">
<el-container  class="home-container">
  <el-header>
    <div>
        <el-avatar :size="60" :src="require('@/assets/ShoppingCart2.png')"></el-avatar>
        <span>手办礼品商城后台系统</span>
    </div>
    <div class="btn"><i class="el-icon-user-solid"></i>{{ admin.username }}<el-button type="info" @click="logout">退出</el-button></div>
    </el-header>
  <el-container>
    <!-- 左侧栏 -->
    <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 侧边栏菜单区域 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
    <el-menu
      unique-opened
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409eff"
      :collapse="isCollapse"
      :collapse-transition="false"
      :default-active="activePath"
      router>
      <!-- 一级菜单 -->
      <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
 <!-- 一级菜单的模块区域 -->
        <template slot="title" >
            <!-- 图标 -->
          <i :class="iconsObj[item.id]" style="font-size: 18px;margin-right: 5px;"></i>
          <!-- 文本 -->
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
        <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNaveState('/'+subItem.path )">
            <template slot="title">
            <!-- 图标 -->
          <i class="el-icon-menu"></i>
          <!-- 文本 -->
          <span>{{subItem.authName}}</span>
        </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <el-container>
      <el-main>
        <router-view :admin="admin"></router-view>
      </el-main>
    </el-container>
  </el-container>
</el-container>
</div>
 
</template>

<script>
import loginAdmin from '@/js/loginAdmin'
export default {
    data(){
        return{
            //左侧菜单数据
          menulist:[{
            id:125,
            children:[{
                id:100,
            children:{},
            authName:'用户列表',
            order:1,
            path:'user'
            }],
            authName:'用户管理',
            order:1,
            path:''
        },
        {
            id:126,
            children:[ 
             { id:101,
            children:{},
            authName:'商品分类',
            order:1,
            path:'categories'
            }, 
           { id:102,
            children:{},
            authName:'分类参数',
            order:1,
            path:'params'
            },
            { id:103,
            children:{},
            authName:'商品列表',
            order:1,
            path:'goods'
            }] ,
            authName:'商品管理',
            order:1,
            path:''
        },
        {
            id:127,
            children:[{
                id:100,
            children:{},
            authName:'订单列表',
            order:1,
            path:'orders'
            }],
            authName:'订单管理',
            order:1,
            path:''
        },{
            id:128,
            children:[
            {
                id:99,
            children:{},
            authName:'商品销售量',
            order:1,
            path:'statistics'
            }
            ],
            authName:'数据统计',
            order:1,
            path:'users'
        }],
        iconsObj:{
        '125':'iconfont icon-yonghuguanli',
        '126':'iconfont icon-shangpinguanli',
        '127':'iconfont icon-tubiao_dingdanguanli',
        '128':'iconfont icon-shujutongji'
        },
        isCollapse:false,
        activePath:'',
        admin:''
        
        }
    },
    methods:{
     async  logout(){
            //   window.sessionStorage.clear()
            const res = await this.$api.rear_end.logout()
           if( res.data.code===1){
            localStorage.clear()
            this.$cookies.remove("admin")      
            this.$message.success('退出成功！') 
            this.$router.go(0) 
              // this.$router.push("/rear-end")
           }else{
            this.$message.error('退出失败！')  
           }
              
        },
         //点击按钮折叠收起
         toggleCollapse(){
         this.isCollapse=!this.isCollapse
        },
        //保存链接的激活状态
        saveNaveState(activePath){
        window.sessionStorage.setItem('activePath',activePath)
        this.activePath=activePath 
      
    },
    
    },
    async  created(){
        const res = await this.$api.rear_end.test()
        loginAdmin.setAdmin(res.data.map.username,res.data.map.adminId,res.data.map.type,res.data.map.createAdminId)
        this.$cookies.set("admin",loginAdmin.admin,60 * 60 * 12)
        this.admin=loginAdmin.admin
        if(loginAdmin.admin.type===3){
       this.menulist=this.menulist.filter(item=> item.authName!="用户管理")
       this.menulist=this.menulist.filter(item=> item.authName!="商品管理")
      }
      },

}
</script>

<style lang="less" scoped>
.home{
    height: 100%;
}
.home-container{
    height: 100%;
}
.el-header{
 background-color:#373d41 ;   
 display: flex;
 justify-content: space-between;
 padding-left: 0px;
 align-items: center;
 color: #fff;
 >div{
    display: flex;
    align-items: center;
 }
>div /deep/ .el-avatar{
    background-color: #f1eaea;
    img{
      transform: translateX(-21px);
    }
 }
 span{
    margin-left: 20px;
    font-size: 22px;
 }
}
.el-aside{
    background-color: #333744;
    .el-menu{
        border-right: none;
    }

}
.el-main{
    background-color: #eaedf1;
}
.toggle-button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
.btn{
  i{
    margin-right: 10px;
  }
  .el-button{
    margin-left: 20px;
  }
}
</style>